<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\web\Response;
use app\models\LoginForm;
use app\models\ContactForm;
use app\services\UsersService;
use app\models\User;

class IndexController extends BaseController {
    public $layout = 'login';
    public $currentHeader = 'header-site';
    public $service = null;

    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    public function init() {
        parent::init();
        $this->service = new UsersService();
    }

    public function actionIndex() {
        if ($this->S('user_id')) {
            return $this->redirect('/home/dashboard');
        }
        $this->model = new User;
        if ($this->I('User')) {
            return $this->actionLogin();
        }
        $this->S(null);
        return $this->show('login');
    }

    public function actionLogin() {
        $email = $this->I('user_email');
        if ($email) {
            $password = $this->I('user_pwd');
            if ($this->service->chkLogin($email, $password)) {
                return $this->redirect(['/home/dashboard']);    
            }
            $this->NG($this->t('common', 'Email or Password not correct!'));
            return $this->show('index');
        }
    }

    public function actionLogout() {
        $this->S(null);
        return $this->redirect(['index/index']);
    }

    /**
     * 记录用户cookie
     */
    public function actionMark() {
        exit('ok');
        echo '<pre>';
        print_r($_SERVER); exit;
    }
}//end
